package com.qqt.csr.common.config;

import com.qqt.csr.common.interceptor.auth.UserAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class RestConfiguration {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(UserAuthInterceptor userAuthInterceptor) {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowCredentials(false)
                        .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS");
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                String[] excludePaths = {"/v3/api-docs/**", "/swagger-resources/**", "/doc.html", "/webjars/**"};
                registry.addInterceptor(userAuthInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns(excludePaths);
            }
        };
    }
}
